package com.pigotech.ptwo.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.pigotech.ptwo.R;
import com.pigotech.ptwo.constant.Constant;
import com.pigotech.ptwo.entity.NormalVideo;

import java.util.List;

/**
 * Created by ghb on 2018/3/29.
 */

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyHolder> {

    private Context mContext;
    private List<NormalVideo> mDatas;
    private OnItemClickListener mItemClickListener;

    public MyRecyclerAdapter(Context context, List<NormalVideo> datas) {
        super();
        this.mContext = context;
        this.mDatas = datas;
    }
    //item的回调接口
    public interface OnItemClickListener{
        void onItemClick(View view,int Position);
    }
    //定义一个设置点击监听器的方法
    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;
    }
    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return mDatas.size();
    }

    @Override
    // 填充onCreateViewHolder方法返回的holder中的控件
    public void onBindViewHolder(final MyHolder holder, final int position) {
        // TODO Auto-generated method stub
        Glide.with(mContext).load("http://" + Constant.IP_ADDRESS + Constant.NORMAL_VIDEO_TUMB_DIRECTORY + mDatas.get(position).getFilename().replace("A.MP4","T.JPG")).into(holder.imageView);
        holder.tv_video_name.setText(mDatas.get(position).getFilename());
        //如果设置了回调，则设置点击事件
        if(mItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    @Override
    // 重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    public MyHolder onCreateViewHolder(ViewGroup viewgroup, int i) {
        // 填充布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_video_list_mini, null);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    // 定义内部类继承ViewHolder
    class MyHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView tv_video_name;

        public MyHolder(View view) {
            super(view);
            imageView = (ImageView) view.findViewById(R.id.iv_normal_video_mini);
            tv_video_name = (TextView) view.findViewById(R.id.tv_video_name);
        }

    }
}
